﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using SQLToolbox.Controller;
using SQLToolbox.Controller.Tools;
using SQLToolbox.View.Types;
using SQLToolbox.Types;
using SQLToolbox.Lib;
namespace SQLToolbox.View.UserControls
{
	/// <summary>
	/// Interaction logic for ucConnectTwoDBs.xaml
	/// </summary>
	public partial class ucConnectTwoDBs : UserControl
	{
		ucDBConnection ctdLeft, ctdRight;
		public ucConnectTwoDBs(DBConnector connector)
		{
			InitializeComponent();
			ctdLeft = new ucDBConnection(connector, "ctdLeft");
			ctdRight = new ucDBConnection(connector, "ctdRight");
			leftPlacholder.Children.Add(ctdLeft);
			rightPlacholder.Children.Add(ctdRight);
		}

		private void btnSwap_Click(object sender, RoutedEventArgs e)
		{
			string tDatasource, tServer, tDatabase = string.Empty;
			ConnectionCredentials tCC;

			tDatasource = ctdLeft.DataSource;
			tServer = ctdLeft.Server;
			tDatabase = ctdLeft.Database;
			tCC = ctdLeft.Credentials;

			ctdLeft.DataSource = ctdRight.DataSource;
			ctdLeft.Server = ctdRight.Server;
			ctdLeft.Database = ctdRight.Database;
			ctdLeft.Credentials = ctdRight.Credentials;

			ctdRight.DataSource = tDatasource;
			ctdRight.Server = tServer;
			ctdRight.Database = tDatabase;
			ctdRight.Credentials = tCC;

		}
		public bool Validate()
		{
			return ctdLeft.Validate() && ctdRight.Validate();
		}
		
		public DBConnectionProperties LeftValue
		{
			get
			{
				return ctdLeft.Value;
			}
		}

		public DBConnectionProperties RightValue
		{
			get
			{
				return ctdRight.Value;
			}
		}

		[DialogValue]
		public Tuple<DBConnectionProperties, DBConnectionProperties> Values
		{
			get
			{
				return Tuple.Create(LeftValue, RightValue);
			}
		}
		[DialogButton("Compare")]//, Number = 0)]
		public bool? OkClicked()
		{
			if (Validate())
				return true;
			else
				MessageBox.Show("Could not connect to databases for comparison", "Connection error",
					MessageBoxButton.OK, MessageBoxImage.Error);
			return null;
			
		}

		[DialogButton("Cancel")]//, Number = 1)]
		public bool? CancelClicked()
		{
			return false;
		}

		public string Title
		{
			get {return "Connect to databases for comparison";}
		}


	
	}
}
